package com.joney.backstage.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.joney.common.annotation.Auth;
import com.joney.backstage.constant.Permissions;
import com.joney.backstage.constant.ReqURLs;
import com.joney.backstage.dto.RoleAddReq;
import com.joney.backstage.dto.RoleEditReq;
import com.joney.backstage.dto.UserRoleRsp;
import com.joney.backstage.entity.Role;
import com.joney.backstage.service.RoleService;
import com.joney.common.pojo.Datagrid;
import com.joney.common.pojo.Result;
import com.joney.common.utils.JwtToken;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author joney123
 * @since 2018-02-07
 */
@Controller
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping(ReqURLs.ROLE_MGR)
    @Auth(Permissions.SYS_ROLE_VIEW)
    public String index(ModelMap model,
                        @RequestParam(required = false) Integer roleId,
                        @RequestParam(required = false) String kind) {
        String url = "sys_base/role_mgr";
        if(kind != null){
            model.put("roleId",roleId);
            if(kind.equals("user")){
                url = "sys_base/role_bind_user";
            }else{
                url = "sys_base/role_bind_res";
            }
            model.put("authMap",roleService.getBindPermission(JwtToken.getLoginUserId().intValue()));
        }else{
            model.put("authMap",roleService.getMgrPermission(JwtToken.getLoginUserId().intValue()));
        }
        return url;
    }

    @GetMapping(ReqURLs.ROLE_LIST)
    @ResponseBody
    @Auth(Permissions.SYS_ROLE_VIEW)
    public Datagrid list(Page page,
                         @RequestParam(required = false) String name,
                         @RequestParam(required = false) Integer state) {
        EntityWrapper wrapper = new EntityWrapper<Role>();
        if(name != null){
            wrapper.like("name",name);
        }
        if(state != null){
            wrapper.eq("state",state);
        }
        Page<Role> pageInfo = roleService.selectPage(page,wrapper);
        return new Datagrid(pageInfo.getTotal(),pageInfo.getRecords());
    }

    @PostMapping(ReqURLs.ROLE_INSERT)
    @ResponseBody
    @Auth(Permissions.SYS_ROLE_ADD)
    public Result insert(@RequestBody @Validated RoleAddReq params) {
        Role temp = new Role();
        BeanUtils.copyProperties(params,temp);
        temp.setCreateUser(JwtToken.getLoginUserId().intValue());
        if(!roleService.insert(temp)){
            return Result.fail();
        }
        return Result.success();
    }

    @PostMapping(ReqURLs.ROLE_UPDATE)
    @ResponseBody
    @Auth(Permissions.SYS_ROLE_EDIT)
    public Result update(@RequestBody @Validated RoleEditReq params) {
        return roleService.updateRole(params,JwtToken.getLoginUserId().intValue());
    }

    @GetMapping(ReqURLs.ROLE_REMOVE)
    @ResponseBody
    @Auth(Permissions.SYS_ROLE_DEL)
    public Result remove(@RequestParam int id) {
        return roleService.deleteRole(id);
    }

    @GetMapping(ReqURLs.ROLE_EDIT)
    @Auth(Permissions.SYS_ROLE_VIEW)
    public String detail(@RequestParam int id,ModelMap model) {
        model.put("data",roleService.selectById(id));
        return "sys_base/role_edit";
    }

    @GetMapping(ReqURLs.ROLE_QUERY_BIND_USER)
    @ResponseBody
    @Auth(Permissions.SYS_ROLE_USER_VIEW)
    public Datagrid queryBindUser(Page page,
                                  @RequestParam int roleId,
                                  @RequestParam int bindState,
                                  @RequestParam(required = false) String userName) {
        Map<String,Object> params = new HashMap<>();
        params.put("roleId",roleId);
        params.put("userName",userName);
        List<UserRoleRsp> list = null;
        if(bindState ==1){ //已绑定的
            list = roleService.queryBind(page,params);
        }else{
            list = roleService.queryNotBindUser(page,params);
        }
        return new Datagrid(page.getTotal(),list);
    }
    @GetMapping(ReqURLs.ROLE_BIND_USER)
    @ResponseBody
    @Auth(Permissions.SYS_ROLE_USER_BIND)
    public Result bindUser(@RequestParam int roleId,
                           @RequestParam int bindState,
                           @RequestParam String userIds) {
        return roleService.bindUser(roleId,bindState,userIds,JwtToken.getLoginUserId().intValue());
    }

    @GetMapping(ReqURLs.ROLE_BIND_RES)
    @ResponseBody
    @Auth(Permissions.SYS_ROLE_RES_BIND)
    public Result bindRes(@RequestParam int roleId,
                          @RequestParam(required = false) String addResIds,
                          @RequestParam(required = false) String delResIds) {
        return roleService.bindRes(roleId,addResIds,delResIds,JwtToken.getLoginUserId().intValue());
    }
}


